package com.supreme.simpleserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "balance")
public class BalanceController {

    @Resource
    private RestTemplate cloudRestTemplate;

    @GetMapping(value = "hello")
    public AjaxResp hello() {
        System.out.println("hello");
        return new AjaxResp("hello");
    }

    @GetMapping(value = "searchProduct")
    public AjaxResp searchProduct() {
        System.out.println("searchProduct");
        return this.cloudRestTemplate.getForObject("http://supreme-business-server/api/product/searchProduct", AjaxResp.class);
    }

    @GetMapping(value = "searchQuality")
    public AjaxResp searchQuality() {
        System.out.println("searchQuality");
        ResponseEntity<AjaxResp> forEntity = this.cloudRestTemplate.getForEntity("http://supreme-business-server/api/quality/searchQuality", AjaxResp.class);
        if (forEntity.getStatusCode().isError()) {
            return null;
        }
        return forEntity.getBody();
    }

    @GetMapping(value = "expressSearchProduct")
    public JSONObject expressSearchProduct() {
        System.out.println("expressSearchProduct");
        ResponseEntity<JSONObject> forEntity = this.cloudRestTemplate.getForEntity("http://supreme-express-server/product/search", JSONObject.class);
        if (forEntity.getStatusCode().isError()) {
            return null;
        }
        return forEntity.getBody();
    }

    @GetMapping(value = "proxySearchAll")
    public JSONObject proxySearchAll() {
        ResponseEntity<JSONObject> forEntity = this.cloudRestTemplate.getForEntity("http://supreme-proxy-server/api/test/searchAll", JSONObject.class);
        if (forEntity.getStatusCode().isError()) {
            return null;
        }
        return forEntity.getBody();
    }
}
